package zpin.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import util.dao.Pagination;
import zpin.dao.CityDao;
import zpin.dao.ResumeDao;
import zpin.dao.TypeDao;
import zpin.model.City;
import zpin.model.Person;
import zpin.model.Resume;
import zpin.model.Type;

import com.opensymphony.xwork2.ActionSupport;


public class ResumeAction extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private Resume resume;
	
	private ResumeDao resumeDao;
	
	private CityDao cityDao;
	
	private TypeDao typeDao;
	
	private List<City> cities;
	
	private List<Type> types;
	
	private Pagination<Resume> pagination;
	
	
	
	

	public Pagination<Resume> getPagination() {
		return pagination;
	}

	public void setPagination(Pagination<Resume> pagination) {
		this.pagination = pagination;
	}

	public List<City> getCities() {
		if (this.cities == null) {
			this.cities = cityDao.findAll();
		}
		return this.cities;
	}

	public void setCities(List<City> cities) {
		this.cities = cities;
	}

	public List<Type> getTypes() {
		if(this.types==null){
			this.types=typeDao.findAll();
		}
		return this.types;
	}

	public void setTypes(List<Type> types) {
		this.types = types;
	}

	public CityDao getCityDao() {
		return cityDao;
	}

	public void setCityDao(CityDao cityDao) {
		this.cityDao = cityDao;
	}

	public TypeDao getTypeDao() {
		return typeDao;
	}

	public void setTypeDao(TypeDao typeDao) {
		this.typeDao = typeDao;
	}

	public Resume getResume() {
		return resume;
	}

	public void setResume(Resume resume) {
		this.resume = resume;
	}

	public ResumeDao getResumeDao() {
		return resumeDao;
	}

	public void setResumeDao(ResumeDao resumeDao) {
		this.resumeDao = resumeDao;
	}
	//登陆简历的方法；
	@Transactional(propagation=Propagation.SUPPORTS)
	public String inputResume(){
		return SUCCESS;
	}
	
	public ResumeAction() {
		this.pagination = new Pagination<Resume>();
		this.pagination.setSize(5);
		this.pagination.setNo(1);
	}
	
	//保存简历
	public String saveResume(){
		//HttpServletRequest request = ServletActionContext.getRequest();
		
		
		
//		Person person = (Person) request.getSession().getAttribute("authorization");
//		
//		resume.setPerson(person);
		resumeDao.saveOrUpdate(resume);
		return SUCCESS;
		
	}
	//通过Resume的id得到对象
	public String updateResume(){
		this.resume=resumeDao.findById((this.resume.getId()));
		return SUCCESS;
	}
	//通过Resume的id得到对象
	public String get(){
		this.resume=resumeDao.findById((resume.getId()));
		return SUCCESS;
	}
	
	public String delete(){
		resumeDao.delete(resume);
		return SUCCESS;
	}
	
	public String browse(){
		resumeDao.browse(pagination);
		return SUCCESS;
	}
	public void validateSaveResume() {
		if(resume.getName().trim().equals("")){
			this.addFieldError("resume.name","姓名不能为空！");
		}
	}

}
